package com.mgr.controller;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.mgr.model.Transactions;
import com.mgr.service.AccountService;
import com.mgr.service.TransactionService;

@Controller
public class ViewTransactionStatistic {

	@Autowired
	private AccountService accountService;

	@Autowired
	private TransactionService transService;

	private ResourceBundle rb = ResourceBundle
			.getBundle("com.mgr.messages.errorMessage");

	@RequestMapping("viewTransactionStatistic")
	public ModelAndView doViewTransaction(HttpSession session) {
		ModelAndView modeView = new ModelAndView();
		String id = (String) session.getAttribute("user");
		if (id == null || id == "") {
			modeView.setViewName("login");
		} else {
			// transService.getTransByTime(1, new Date() ,new Date());
			modeView.setViewName("user/viewTransactionStatistic");
			modeView.addObject("listAccId",
					accountService.getAccountByCustId(Integer.parseInt(id)));
		}
		return modeView;

	}

	@RequestMapping("doViewTrans")
	public ModelAndView doViewTrans(HttpSession session,
			HttpServletRequest request) {
		ModelAndView modeView = new ModelAndView();
		String id = (String) session.getAttribute("user");
		if (id == null || id == "") {
			modeView.setViewName("login");
		} else {
			String startDate = request.getParameter("startDate");
			String endDate = request.getParameter("endDate");
			int accId = Integer.parseInt(request.getParameter("account"));
			System.out.println(startDate);
			SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yyyy");
			try {
				Date sd = formatter.parse(startDate);
				Date ed = formatter.parse(endDate);
				System.out.println("" + sd);
				modeView.addObject("lstTrans",
						transService.getTransByTime(accId, sd, ed));
			} catch (ParseException e) {
				System.out.println("error");
			}

			modeView.setViewName("user/viewTransactionStatistic");
			modeView.addObject("listAccId",
					accountService.getAccountByCustId(Integer.parseInt(id)));
		}
		return modeView;

	}

}
